using System;
using System.Collections.Generic;
using System.Text;
using SpriteCraft;
using System.Drawing;

namespace SpaceGame
{
  public abstract class Player : IOnUpdate, IOnCollide, IOnLifetimeOut
  {
    protected EngineClass m_Engine;
    protected Weapon m_Weapon;
    protected Ship m_Ship;
    protected Stage m_Stage;

    //Ship movement directions: left or right, up or down.
    protected long leftright;
    protected long updown;

    protected bool m_isDestroyed;

    public Player(EngineClass engine, Stage stage)
    {
      m_isDestroyed = false;

      m_Engine = engine;
      m_Stage = stage;
    }

    static int counter = 0;
    public void Explode(ISprite self)
    {
      IParticles p = null;

      //Create new paticles system
      p = m_Engine.NewParticles("expl" + self.name, "explosive", 2);

      p.subimpl = this;

      //This is time interval between tow consecutive emissions
      p.pulse = 33;

      //The number of particles emitted in one pulse
      p.pulseCount = 6;

      //The total number of particles
      p.count = 18;

      //Environmental attributes
      p.scaleDelta = self.random * 50 + 10;
      p.angleDelta = self.random * 3.14f;
      p.transparencyDelta = 150;

      //Overall luminance  
      p.luminance = self.random * 50 + 50;

      //After 700 ms after creation the particle system will become inactive
      p.lifetime = 700;

      //The system is not cycled: after all particles are used, 
      //the generation point becomes inactive
      p.cycled = false;

      //Start attributes for particles
      p.SetStartDirection(-3.14f, 3.14f);
      p.SetStartSpeed(30, 90);

      //Activate system
      p.active = true;

      //Generation point coordinates
      p.x = self.x;
      p.y = self.y;

      p.visible = true;
    }

    #region Interfaces

    public abstract void OnUpdate(ISprite sprite, int tickdelta);

    public abstract void OnCollide(ISprite self, ISprite Hit);

    public void OnLifetimeOut(IParticles particles)
    {
      particles.Dispose();
    }
    
    #endregion
  }
  public class CharacterPlayer : Player
  {
    public CharacterPlayer(EngineClass engine, Stage stage)
      : base(engine, stage)
    {
      m_Weapon = new LaserWeapon(m_Engine, Program.PlayerShotCollideGroup);
      m_Ship = new SmallFighterShip(m_Engine, this, Program.PlayerShipCollideGroup, new Point(0, m_Engine.scrHeight/2));
    }

    #region Interfaces

    public override void OnUpdate(ISprite sprite, int tickdelta)
    {
      leftright = 0;
      updown = 0;

      //We're using default key mapping rules (see the key definitions above).
      if (m_Engine.IsKeyPressed(tagKeyVal.Key_StLeft)) { leftright = -1; }
      if (m_Engine.IsKeyPressed(tagKeyVal.Key_StRight)) { leftright = leftright + 1; }
      if (m_Engine.IsKeyPressed(tagKeyVal.Key_StUp)) { updown = -1; }
      if (m_Engine.IsKeyPressed(tagKeyVal.Key_StDown)) { updown = updown + 1; }

      m_Stage.DoStuff(tickdelta);

      m_Ship.DoStuff(m_Stage, tickdelta, leftright, updown);

      m_Weapon.DoStuff(m_Ship, tickdelta);
    }

    public override void OnCollide(ISprite self, ISprite Hit)
    {
      if (!m_isDestroyed)
      {
        m_isDestroyed = true;
        //this.Explode(self);

        //self.Dispose();
      }
    }

    #endregion
  }
}
